﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class AddEmployeeDialog : ChildWindow
    {
        public AddEmployeeDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Ensure that a first and last name have been entered
            if (tbFirstName.Text.Trim().Length > 0 && tbLastName.Text.Trim().Length > 0)
            {
                // Create new employee/login objects
                ServiceReferenceOpenRPOS.Login login = new ServiceReferenceOpenRPOS.Login();
                // Set the default display name/user name
                login.bFilterDisplayName = true;
                login.col_DisplayName = tbFirstName.Text.Trim() + " " + (tbMiddleInitial.Text.Trim().Length == 1 ? tbMiddleInitial.Text.Trim() + ". " : " ") + tbLastName.Text.Trim();
                login.bFilterUserName = true;
                login.col_UserName = tbFirstName.Text.Substring(0, 1) + tbLastName.Text.Trim();
                login.bFilterManager = true;
                login.col_Manager = false;
                ServiceReferenceOpenRPOS.Employee employee = new ServiceReferenceOpenRPOS.Employee();
                employee.bFilterFirstName = true;
                employee.col_FirstName = tbFirstName.Text.Trim();
                employee.bFilterMiddleInitial = true;
                employee.col_MiddleInitial = tbMiddleInitial.Text.Trim();
                employee.bFilterLastName = true;
                employee.col_LastName = tbLastName.Text.Trim();
                employee.bFilterLoginID = true;
                employee.bFilterActive = true;
                employee.col_Active = true;
                // Save the login object...
                employee.col_LoginID = await Globals.db.AddLoginObject(login);
                // Save employee information
                await Globals.db.AddEmployeeObject(employee);

                // Reload the list...
                Globals.employees.strSelectedName = tbLastName.Text.Trim() + ", " + tbFirstName.Text.Trim() + " " + tbMiddleInitial.Text.Trim();
                Globals.employees.init();
                this.DialogResult = true;
            }
            else
                MessageBox.Show("Please provide a first and last name.");
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbLastName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

